//
//  MyImagePickerManager.m
//  OntheRoadV4
//
//  Created by hz on 4/7/13.
//  Copyright (c) 2013 EricHuang. All rights reserved.
//

#import <UIKit/UIImagePickerController.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import <ImageIO/ImageIO.h>
#import "QWImagePickerManager.h"
#import "ALAssetsLibrary+CustomPhotoAlbum.h"
#import "ALAsset+Downscaling.h"

@interface QWImagePickerManager ()<UIImagePickerControllerDelegate,
UINavigationControllerDelegate,
QWImagePickerControllerDelegate,
AlbumViewControllerDelegate,
ImagePreviewViewControllerDelegate>
{
    
}

@property (nonatomic, retain) NSArray *selectedImgArr;
@property (nonatomic, strong)   AlbumViewController *albumPicker;
@property (nonatomic, strong)   QWImagePickerControllerViewController *cameraPicker;


@end

@implementation QWImagePickerManager

@synthesize cameraPicker;


/**
 *
 *  初始化QWImagePickerManager
 *  @param maxCnt   最多能够选择几张图片
 *  @param userInfo 用户标识信息
 *  @param delegate 代理
 *
 *  @return QWImagePickerManager 实例
 */
- (id)initWithMaxImageCnt:(NSUInteger)maxCnt
         specificUserInfo:(id)userInfo
                 delegate:(id<QWImagePickerManagerDelegate>)delegate
{
    
    self = [super init];
    if (self) {
        _maxImageCnt = maxCnt;
        self.userInfo = userInfo;
        self.delegate = delegate;
    }
    return self;
}

/**
 *
 *  初始化QWImagePickerManager
 *  @param maxCnt       最多能够选择几张图片
 *  @param selectedImgs 已经选择的资源
 *  @param delegate     代理
 *
 *  @return QWImagePickerManager 实例
 */
- (id)initWithMaxImageCnt:(NSUInteger)maxCnt
             selectedImgs:(NSArray *)selectedImgs
                 delegate:(id<QWImagePickerManagerDelegate>)delegate
{
    self = [super init];
    if (self) {
        _maxImageCnt = maxCnt;
        _selectedImgArr = selectedImgs;
        self.delegate = delegate;
    }
    return self;
    
}



- (void)presentCameraOn:(UIViewController *)vc forPhotos:(BOOL)forPhotos Animated:(BOOL)animated
{
    _albumPicker = [[AlbumViewController alloc] initWithMaxImageCnt:_maxImageCnt forPhotos:forPhotos];
    _albumPicker.delegate = self;
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:_albumPicker];

    //PortraitNavgationController *nav = [[PortraitNavgationController alloc] initWithRootViewController:_albumPicker];
    [vc presentViewController:nav animated:YES completion:^{
        
    }];
}
         
- (void)dismissCameraAnimated:(BOOL)animated
{
    [self.albumPicker dismissViewControllerAnimated:YES
                                         completion:^{
                                             
                                         }];
    self.albumPicker = nil;
    self.cameraPicker = nil;
}

#pragma mark -
#pragma mark AlbumViewControllerDelegate

- (void)albumViewControllerShowCameraView
{
    if (!self.cameraPicker) {
        self.cameraPicker = [[QWImagePickerControllerViewController alloc] initWithCaptureManager:self.albumPicker.captureManager
                    previewLayer:self.albumPicker.previewLayer];
        self.cameraPicker.delegate = self;
        self.cameraPicker.maxImageCnt = self.maxImageCnt;
    }
    
    [self.albumPicker.navigationController pushViewController:self.cameraPicker
                                                     animated:YES];
}

- (void)didSkipCamera
{
    if ([self.delegate respondsToSelector:@selector(QWImagePickerManagerDidSkip:)]) {
        [self.delegate QWImagePickerManagerDidSkip:self];
    } else {
        [self dismissCameraAnimated:YES];
    }
}

- (void)didCancelAlbumPickerAnimated:(BOOL)animated
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(QWImagePickerManagerDidCancel:)]) {
        [self.delegate QWImagePickerManagerDidCancel:self];
    } else {
        [self dismissCameraAnimated:animated];
    }
}

- (void)didFinishPickingImageAssets:(NSArray *)selectedAssets
{
    if ([self.delegate respondsToSelector:@selector(QWImagePickerManager:didPickImageAssets:)]) {
        [self.delegate QWImagePickerManager:self
                         didPickImageAssets:selectedAssets];
    } else {
        [self dismissCameraAnimated:YES];
    }
}

- (void)didFinishPickingVideoWithDict:(NSDictionary *)dict
{
    if ([self.delegate respondsToSelector:@selector(QWImagePickerManager:didPickVideo:)]) {
        [self.delegate QWImagePickerManager:self didPickVideo:dict];
    } else {
        [self dismissCameraAnimated:YES];
    }
}

#pragma mark -
#pragma mark QWImagePickerControllerDelegate

- (void)didFinishTakeAImage:(NSDictionary *)userInfo
{
    UIImage *image = [userInfo objectForKey:@"Image"];
    NSDictionary *metaData = [userInfo objectForKey:@"metadata"];

    ImagePreviewViewController *previewVC = [[ImagePreviewViewController alloc] initWithImage:image metaData:metaData];
    
    previewVC.delegate = self;
    previewVC.hidesBottomBarWhenPushed = YES;

    [self.albumPicker.navigationController pushViewController:previewVC animated:YES];
}

- (void)didCancelCameraAnimated:(BOOL)animated
{
    [self.albumPicker.navigationController popToViewController:self.albumPicker
                                                      animated:animated];
    
    // 重新吧CaptureVideoPreviewLayer加到第一个cell上。
    [self.albumPicker resetCaptureVideoPreviewLayer];
    self.cameraPicker = nil;
}

#pragma mark -
#pragma mark implement ImagePreviewViewControllerDelegate

- (void)DidSaveImageWithInfo:(NSDictionary *)info
{
    if (self.albumPicker) {
        
        id infoAsset = [info objectForKey:@"asset"];
        
        if ([infoAsset isKindOfClass:[ALAsset class]])
        {
            ALAsset  *asset = (ALAsset*)infoAsset;
            [self.albumPicker takeAPicture:[asset.defaultRepresentation url]];
        }else
        {
            PHAsset  *asset = (PHAsset*)infoAsset;
            [self.albumPicker takeAPicture:asset.localIdentifier];
        }
        [self.albumPicker.navigationController popToViewController:self.albumPicker
                                                          animated:YES];
    }
    
    self.cameraPicker = nil;
}


- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end